package com.rybbaby.tss.tests.validator;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.rybbaby.tss.core.annotation.validator.Base64Validator;
import com.rybbaby.tss.core.annotation.validator.BetweenValidator;
import com.rybbaby.tss.core.annotation.validator.CreditCardValidator;
import com.rybbaby.tss.tests.validator.controller.ValidatorTempClass;
import com.rybbaby.tss.core.annotation.validator.CustomValidator;
import com.rybbaby.tss.core.annotation.validator.DateValidator;
import com.rybbaby.tss.core.annotation.validator.EmailAddressValidator;
import com.rybbaby.tss.core.annotation.validator.IPValidator;
import com.rybbaby.tss.core.annotation.validator.PhoneValidator;
import com.rybbaby.tss.core.annotation.validator.StringValidator;
import com.rybbaby.tss.core.verifier.AbstractVerifier;

/**
 * @description Validator 测试
 * 
 * @author 谢进伟
 * 
 * @createDate 2016年3月29日 下午3:04:44
 */
public class ValidatorTets {
	
	/**
	 * @param args
	 */
	public static void main(String [] args) {
		try {
			Class<ValidatorTempClass> cls = ValidatorTempClass.class;
			Base64ValidatorTest("name" , "asdfasdddddddfasdfasdf==" , cls);
			BetweenValidatorTest("age" , 15 , cls);
			CreditCardValidatorTest("creditCard" , "6253624002700076" , cls);
			CreditCardValidatorTest("creditCard" , "6228370026658138" , cls);
			DateVerifierValidatorTest("date" , "2015-03-31 10:13:50" , cls);
			EamilAddressVerifierValidatorTest("email" , "xiejinwei13420@gmail.com" , cls);
			IPVerifierValidatorTest("ip" , "192.168.1.1" , cls);
			PhoneVerifierValidatorTest("phone" , "12222222222" , cls);
			StringVerifierValidatorTest("remark" , "你好好" , cls);
			CustomVerifierValidatorTest("userName" , "admin1" , cls);
			System.out.println("------------");
			Object a , b;
			a = 3;
			b = a;
			a = 4;
			System.out.println(b);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static void Base64ValidatorTest(String fieldName , Object value , Class<ValidatorTempClass> cls) throws NoSuchFieldException , InstantiationException , IllegalAccessException , NoSuchMethodException , InvocationTargetException {
		Field field = cls.getDeclaredField(fieldName);
		Base64Validator annotation = field.getDeclaredAnnotation(Base64Validator.class);
		Class<? extends AbstractVerifier> validator = annotation.verifier();
		AbstractVerifier obj = validator.newInstance();
		
		initVerifierValue(annotation , validator , obj);
		
		Method method = validator.getDeclaredMethod("validate" , Object.class);
		Object result = method.invoke(obj , value);
		System.out.println(result);
	}
	
	private static void BetweenValidatorTest(String fieldName , Object value , Class<ValidatorTempClass> cls) throws NoSuchFieldException , InstantiationException , IllegalAccessException , NoSuchMethodException , InvocationTargetException {
		Field field = cls.getDeclaredField(fieldName);
		BetweenValidator annotation = field.getDeclaredAnnotation(BetweenValidator.class);
		Class<? extends AbstractVerifier> validator = annotation.verifier();
		AbstractVerifier obj = validator.newInstance();
		
		initVerifierValue(annotation , validator , obj);
		
		Method method = validator.getDeclaredMethod("validate" , Object.class);
		Object result = method.invoke(obj , value);
		System.out.println(result);
	}
	
	private static void CreditCardValidatorTest(String fieldName , Object value , Class<ValidatorTempClass> cls) throws NoSuchFieldException , InstantiationException , IllegalAccessException , NoSuchMethodException , InvocationTargetException {
		Field field = cls.getDeclaredField(fieldName);
		CreditCardValidator annotation = field.getDeclaredAnnotation(CreditCardValidator.class);
		Class<? extends AbstractVerifier> validator = annotation.verifier();
		AbstractVerifier obj = validator.newInstance();
		
		initVerifierValue(annotation , validator , obj);
		
		Method method = validator.getDeclaredMethod("validate" , Object.class);
		Object result = method.invoke(obj , value);
		System.out.println(result);
	}
	
	private static void DateVerifierValidatorTest(String fieldName , Object value , Class<ValidatorTempClass> cls) throws NoSuchFieldException , InstantiationException , IllegalAccessException , NoSuchMethodException , InvocationTargetException {
		Field field = cls.getDeclaredField(fieldName);
		DateValidator annotation = field.getDeclaredAnnotation(DateValidator.class);
		Class<? extends AbstractVerifier> validator = annotation.verifier();
		AbstractVerifier obj = validator.newInstance();
		
		initVerifierValue(annotation , validator , obj);
		
		Method method = validator.getDeclaredMethod("validate" , Object.class);
		Object result = method.invoke(obj , value);
		System.out.println(result);
	}
	
	private static void EamilAddressVerifierValidatorTest(String fieldName , Object value , Class<ValidatorTempClass> cls) throws NoSuchFieldException , InstantiationException , IllegalAccessException , NoSuchMethodException , InvocationTargetException {
		Field field = cls.getDeclaredField(fieldName);
		EmailAddressValidator annotation = field.getDeclaredAnnotation(EmailAddressValidator.class);
		Class<? extends AbstractVerifier> validator = annotation.verifier();
		AbstractVerifier obj = validator.newInstance();
		
		initVerifierValue(annotation , validator , obj);
		
		Method method = validator.getDeclaredMethod("validate" , Object.class);
		Object result = method.invoke(obj , value);
		System.out.println(result);
	}
	
	private static void IPVerifierValidatorTest(String fieldName , Object value , Class<ValidatorTempClass> cls) throws NoSuchFieldException , InstantiationException , IllegalAccessException , NoSuchMethodException , InvocationTargetException {
		Field field = cls.getDeclaredField(fieldName);
		IPValidator annotation = field.getDeclaredAnnotation(IPValidator.class);
		Class<? extends AbstractVerifier> validator = annotation.verifier();
		AbstractVerifier obj = validator.newInstance();
		
		initVerifierValue(annotation , validator , obj);
		
		Method method = validator.getDeclaredMethod("validate" , Object.class);
		Object result = method.invoke(obj , value);
		System.out.println(result);
	}
	
	private static void PhoneVerifierValidatorTest(String fieldName , Object value , Class<ValidatorTempClass> cls) throws NoSuchFieldException , InstantiationException , IllegalAccessException , NoSuchMethodException , InvocationTargetException {
		Field field = cls.getDeclaredField(fieldName);
		PhoneValidator annotation = field.getDeclaredAnnotation(PhoneValidator.class);
		Class<? extends AbstractVerifier> validator = annotation.verifier();
		AbstractVerifier obj = validator.newInstance();
		
		initVerifierValue(annotation , validator , obj);
		
		Method method = validator.getDeclaredMethod("validate" , Object.class);
		Object result = method.invoke(obj , value);
		System.out.println(result);
	}
	
	private static void StringVerifierValidatorTest(String fieldName , Object value , Class<ValidatorTempClass> cls) throws NoSuchFieldException , InstantiationException , IllegalAccessException , NoSuchMethodException , InvocationTargetException {
		Field field = cls.getDeclaredField(fieldName);
		StringValidator annotation = field.getDeclaredAnnotation(StringValidator.class);
		Class<? extends AbstractVerifier> validator = annotation.verifier();
		AbstractVerifier obj = validator.newInstance();
		
		initVerifierValue(annotation , validator , obj);
		
		Method method = validator.getDeclaredMethod("validate" , Object.class);
		Object result = method.invoke(obj , value);
		System.out.println(result);
	}
	
	private static void CustomVerifierValidatorTest(String fieldName , Object value , Class<ValidatorTempClass> cls) throws NoSuchFieldException , InstantiationException , IllegalAccessException , NoSuchMethodException , InvocationTargetException {
		Field field = cls.getDeclaredField(fieldName);
		CustomValidator annotation = field.getDeclaredAnnotation(CustomValidator.class);
		Class<? extends AbstractVerifier> validator = annotation.verifier();
		AbstractVerifier obj = validator.newInstance();
		
		initVerifierValue(annotation , validator , obj);
		
		Method method = validator.getDeclaredMethod("validate" , Object.class);
		Object result = method.invoke(obj , value);
		System.out.println(result);
	}
	
	private static void initVerifierValue(Object annotation , Class<? extends AbstractVerifier> verifier , AbstractVerifier obj) throws IllegalAccessException , InvocationTargetException {
		Field [] fields = verifier.getDeclaredFields();
		Method [] annoMethods = annotation.getClass().getDeclaredMethods();
		for(Field f : fields) {
			String fieldName1 = f.getName();
			for(Method method : annoMethods) {
				String methodName = method.getName();
				if(fieldName1.equals(methodName)) {
					f.setAccessible(true);
					method.setAccessible(true);
					f.set(obj , method.invoke(annotation));
					break;
				}
			}
		}
	}
}
